<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            选择工单
        </div>
    </header>
    <script id="woTemp" type="text/x-dot-template">

			{{~ it:value:index}}
			<li class="aui-list-item" onclick="setWo('{{= it[index].workOrderId }}','{{= it[index].workOrderCode }}');">
					<div class="aui-list-item-inner">
							<div class="aui-list-item-title">{{= it[index].workOrderCode }}</div>
					</div>
			</li>
			{{~}}

	</script>
    <div class="aui-content aui-margin-b-15" style="margin-top:60px;">


        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">生产线编码</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="扫描/选择生产线" id="workCenterName" value=""
                            onclick="getIdSelector();" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCenter()"></i>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">生产线编码</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="扫描/选择生产线" id="workCenterCode" value=""
                            onclick="getIdSelector();" />
                    </div>

                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">计划日期</div>
                    <div class="aui-list-item-input" onclick="timePicker();">
                        <input type="text" placeholder="选择日期" class="aui-input" id="plandate">
                    </div>
                </div>
            </li>
        </ul>
        <ul class="aui-list aui-list-in" id="workOrderListUl">

        </ul>
        <div style="width:100%;margin-top:1%;"></div>
        <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;margin-bottom:10px;">
            <table class="table" style='width:100%;' id="table">
                <thead>
                    <tr
                        style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 18px;font-weight: bold;height:100%;">
                        <td algin='center'>工单号</td>
                        <td algin='center'>零件号</td>
                        <td algin='center'>数量</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtml">
                </tbody>
            </table>
        </div>
    </div>
    </div>

    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var workCenterCode = "";
    var workCenterName = "";
    var workOrderCode = "";
    apiready = function () {
        $("#workCenterName").focus();
        idSelector = "workCenterName";

        crateDate();
        getWorkOrderByMrl();
    };

    function crateDate() {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        $("#plandate").val(year + "-" + month + "-" + day);
    }

    //时间选择器
    function timePicker(id) {
        api.openPicker({
            type: 'date',
            date: new Date(),
            title: '选择日期'
        }, function (ret, err) {
            if (ret) {
                var monthTemp = (ret.month < 10 ? '0' + ret.month : ret.month);
                var dayTemp = (ret.day < 10 ? '0' + ret.day : ret.day);
                var date = ret.year + "-" + monthTemp + "-" + dayTemp;
                $("#plandate").val(date);
                getWorkOrderByMrl();
            }
        });
    }
    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }
    //查询扫描规则
    function initScanRule() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getTmScanRule";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    scanRule = myobj.scanRule
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }
    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            //alert(ret);扫码的结果都是编码
            //1：产线
            if (idSelector == 'workCenterName') {

                $("#workCenterName").val(ret);
                //查询产线
                getWorkCell(ret, idSelector);

            }
        });
    }
    //产线回车事件
    $("#workCenterName").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var workCenterName = $("#workCenterName").val();
        var inputId = 'workCenterName'
        if (keycode == '13') {
            var serverUrl = localStorage.getItem('serverUrl');
            $("#workCenterName").val(workCenterName);
            var url = serverUrl + "/app/getWorkCenterH";
            var data = {
                values: {

                    workCenterName: workCenterName,

                }
            };
            api.ajax({
                url: url,
                method: 'post',
                dataType: 'json',
                timeout: 30,
                returnAll: false,
                data: data
            }, function (ret, err) {
                if (ret) {
                    var data = JSON.stringify(ret);
                    var myobj = eval("(" + data + ")");
                    if (myobj.code == '0') {
                        var list = eval(myobj.list);
                        workCenterCode = list[0].workCenterCode;
                        workCenterName = list[0].workCenterCode + ":" + list[0].workCenterName;
                        document.getElementById(inputId).value = list[0].workCenterCode + ":" + list[0].workCenterName;
                        getWorkOrderByMrl();

                    } else {
                        api.toast({
                            msg: myobj.message,
                            duration: 3000
                        });
                        $("#workCenterName").focus();
                        $("#workCenterName").select();

                    }
                }
            });

            getWorkOrderByMrl();
        }
    });
    //选择生产线
    function goSelectWorkCenter() {
        api.openWin({
            name: 'uwmWorkCenter-new',
            url: 'uwmWorkCenter-new.html',
            pageParam: {
                flag: "in",
                name: "selWorkOrderListNew",
                mrlCode: "",
                lotCode: "",
                qty: ""
            }
        });
    }
    //5:库位扫描校验
    function getWorkCell(code, inputId) {
        var un = localStorage.getItem("un");

        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCentercode";
        var data = {
            values: {

                workCenterCode: code,

            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    workCenterCode = list[0].workCenterCode;
                    workCenterName = list[0].workCenterName;
                    document.getElementById(inputId).value = list[0].workCenterCode + ":" + list[0].workCenterName;
                    getWorkOrderByMrl();

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $("#workCenterName").focus();
                    $("#workCenterName").select();

                }
            }
        });
    }

    //生产线选择后回调函数
    function setWorkCenter(id, code, name) {
        workCenterCode = code;
        workCenterName = name;
        $("#workCenterName").focus();
        document.getElementById('workCenterName').value = code + ":" + name;
        getWorkOrderByMrl();
    }

    function getWorkOrderByMrl() {
        var un = localStorage.getItem('un');
        var pw = localStorage.getItem('pw');
        var serverIp = localStorage.getItem('serverIp');
        var serverPort = localStorage.getItem('serverPort');
        var serverProject = localStorage.getItem('serverProject');
        var plandate = $("#plandate").val();

        var params = new Object();
        params.method = 'post';
        params.dataType = 'json';
        params.returnAll = 'false';
        params.timeout = 60;


        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkOrderByMrl";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                un: un,
                pw: pw,
                mrlCode: "",
                workCenterId: "",
                plandate: plandate,
                workCenterCode: workCenterCode,

            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 60,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var list = myobj.list;
                    var html = "";

                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr style='text-align: center;background-color: #F5F5F5;color: black;font-size: 10px;height:22px;'>";
                        html += "<td name='workOrderCode' onclick=setWo('" + list[i].workOrderCode + "')>" + list[i].workOrderCode + "</td>";
                        html += "<td name='produCode'>" + list[i].produCode + "</td>";
                        html += "<td name='qty'>" + list[i].qty + "</td>";

                        html += "</tr>";
                    }

                    $api.html($api.byId("tbodyHtml"), html);
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    function setWo(code) {
        var name = api.pageParam.name;
        console.log(code)
        api.closeWin();
        api.execScript({
            name: name,

            script: 'setWo("' + code + '")'
        });
    }

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>